package com.cy.pj.goods.controller;


import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("/goods/")
public class GoodsController {

    @Autowired//has a
    private GoodsService goodsService;

    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id,Model model){
        Goods goods=goodsService.findById(id);
        model.addAttribute("g", goods);
        return "goods-update";
    }

    @RequestMapping("doUpdateGoods")
    public String doUpdateGoods(Goods entity){//用pojo对象接收客户端参数（参数名需要与entity对象中的set方法相匹配）
        goodsService.updateGoods(entity);
        return "redirect:/goods/doGoodsUI";
    }

    @RequestMapping("doSaveGoods")
    public String doSaveGoods(Goods entity){//用pojo对象接收客户端参数（参数名需要与entity对象中的set方法相匹配）
        goodsService.saveGoods(entity);
        return "redirect:/goods/doGoodsUI";
    }

    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI(){
        return "goods-add";
    }

    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id){
        goodsService.deleteById(id);
        return "redirect:/goods/doGoodsUI";
    }


    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model){
        List<Goods> goodsList=goodsService.findGoods();
        model.addAttribute("goodsList",goodsList);
        return "goods";//viewname
        //返回值会交给dispatcherservlet进行处理
        //dispatcherservlet会调用viewresolver进行视图解析（view+model）
        //最后
    }

}
